package com.zhuss.opus.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.user.UpdateUserBo;
import com.zhuss.bean.bo.opus.opus.DeleteOpusAppendBo;
import com.zhuss.bean.bo.opus.opus.wechat.ThumbOpusBo;
import com.zhuss.bean.vo.core.user.DetailUserVo;
import com.zhuss.bean.vo.opus.opus.DetailOpusAppendVo;
import com.zhuss.bean.vo.opus.opus.DetailOpusVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.processor.UserRead;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 取消点赞作品 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("opusDelThumb")
public class OpusDelThumb extends AbstractProcessor<Object, ThumbOpusBo> implements Processor<Object, ThumbOpusBo> {

    @Autowired
	@Qualifier("opusRead")
	private OpusRead opusRead;
    
    @Autowired
    @Qualifier("userRead")
    private UserRead userRead;
    
    @Autowired
	@Qualifier("opusAppendRead")
	private OpusAppendRead opusAppendRead;
    
    @Autowired
    @Qualifier("opusAppendDelete")
    private Processor<Object, DeleteOpusAppendBo> opusAppendDelete;
    
    @Autowired
    @Qualifier("userUpdate")
    private Processor<Object, UpdateUserBo> userUpdate;
    
    /**
     * 校验
     */
    @Override
    public void check(ThumbOpusBo bo) throws WpsException {
    	Long opusId = bo.getOpusId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailOpusAppendVo result = opusAppendRead.getByType(appendType, opusId, bo.getConcurrentUserId());
    	
    	if (result == null) {
    		throw new WpsException(ExceptionCode.作品未点赞);
    	}
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(ThumbOpusBo param) throws WpsException {
    	Long opusId = param.getOpusId();
    	Integer appendType = param.getAppendType();
    	
    	DetailOpusAppendVo result = opusAppendRead.getByType(appendType, opusId, param.getConcurrentUserId());
    	
    	DetailOpusVo opus = ((DetailOpusVo) opusRead.detail(opusId));
    	String name = opus.getTitle();
    	
    	DeleteOpusAppendBo bo = new DeleteOpusAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	bo.setOpusAppendId(result.getOpusAppendId());
    	opusAppendDelete.process(bo);
    	
    	
    	Long userId = opus.getCreatorId();
    	
    	DetailUserVo user = (DetailUserVo) userRead.detail(userId);
    	
    	UpdateUserBo updateUserBo = new UpdateUserBo();
    	BeanUtils.copyProperties(param, updateUserBo);
    	BeanUtils.copyProperties(user, updateUserBo);
    	updateUserBo.setUserId(userId);
    	updateUserBo.setAddThumb(-1);
    	userUpdate.process(updateUserBo);
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(ThumbOpusBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.作品.key());
    	record.setAction(LogActionEnum.作品取消点赞.key());
    	record.setTargetId(param.getOpusId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
